<?php
define('DISABLEDEFENSE', true);
define('DISABLEXSSCHECK', true);

require '../../../../class/class_core.php';
C::app()->init();
require_once "hupijiao_api.php";

global $_G;

define('BOAN_PAYMENT_NAME', 'plugin/boan_payment');


if(empty($_G['cache']['plugin'])){
    loadcache('plugin');
}

$vars = $_G['cache']['plugin']['boan_payment'];
$appid     = $vars['hpjpay_appid'];
$appsecret = $vars['hpjpay_appsecret'];
$plugin_id = 'boan_payment';
$gateway   = $vars['hpjpay_gateway'];

$data = $_GET;
foreach ($data as $k=>$v){
    $data[$k] = stripslashes($v);
}

if($data['attach'] == 'alipay'){
    $appid     = $vars['hpjpay_alipay_appid'];
    $appsecret = $vars['hpjpay_alipay_appsecret'];
}

if(!isset($data['hash']) || !isset($data['trade_order_id'])){
    echo 'failed';
    exit;
}

if(isset($data['plugins']) && $data['plugins'] != $plugin_id){
    echo 'failed';
    exit;
}

$appkey =$appsecret;
$hash =XH_Payment_Api::generate_xh_hash($data,$appkey);

if($data['hash'] != $hash){
    echo 'failed';
    exit;
}

$trade_order_id =$data['trade_order_id'];

if($data['status']=='OD'){
    $order = C::t('#boan_payment#boan_payment_order')->fetch($trade_order_id);
    $order_id = $order['order_id'];
    $lockid = 'pay_'.$order_id;
    if(!discuz_process::islocked($lockid)){
        if($order['status'] == 0){
            $pay_method = 'wxpay';
            if(!empty($data['attach']) && ($data['attach'] == 'wxpay' || $data['attach'] == 'alipay')){
                $pay_method = $data['attach'];
            }
            C::m('#boan_payment#payment')->updateOrder($order_id,['pay_method'=>$pay_method,'status'=>1,'pay_time'=>$data['time'],'trade_id'=>$data['transaction_id']]);
            C::m('#boan_payment#payment')->log($data['transaction_id'],2,'wxpay',lang(BOAN_PAYMENT_NAME, 'order_info1'), 2,$data);
            C::m('#boan_payment#payment')->pluginNotify($order_id,'payNotify');
        }
        discuz_process::unlock($lockid);
    }
}

echo 'success';
exit;



